﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using VMExplorer.Win32;

namespace VMExplorer.MemoryObjects
{
  class LdrDataObject : RemoteMemoryObject<Native.PEB_LDR_DATA>
  {
    #region Construction

    private LdrDataObject (SafeProcessHandle hProcess, MemoryAddress baseAddress)
      : base(hProcess, baseAddress, RegionUsage.System)
    {
      Name = "LoaderData";
    }

		public static IEnumerable<IMemoryObject> Create (SafeProcessHandle hProcess, MemoryAddress baseAddress)
    {
      List<IMemoryObject> memObjects = new List<IMemoryObject> ();

      if (baseAddress.IsValid)
      {
        #region :: LoaderData ::

        LdrDataObject ldw = new LdrDataObject(hProcess, baseAddress);
        memObjects.Add(ldw);

        #endregion
        #region :: Modules ::

        ListEntry inLoadOrderModuleList = new ListEntry (hProcess, ldw.Inner.InLoadOrderModuleList);
        foreach(MemoryAddress ma in inLoadOrderModuleList.ForwardList())
        {
					if (ma.IsValid)
					{
						LdrModule hvaw = new LdrModule (hProcess, ma);
            ModuleObject pmo = new ModuleObject (hProcess, hvaw);
            memObjects.Add (pmo);
					}
				}
				
				//vaList = ldw.InMemoryOrderModuleList.ForwardList ();
				//for (int index = 0; index < vaList.Count - 1; index++)
				//{
				//  LdrModuleWrapper hvaw = new LdrModuleWrapper (hProcess, (MemoryAddress)vaList [index] - 8);
				//  memObjects.Add (hvaw.ModuleWrapper);
				//}
				
				//vaList = ldw.InInitializationOrderModuleList.ForwardList ();
				//for (int index = 0; index < vaList.Count - 1; index++)
				//{
				//  LdrModuleWrapper hvaw = new LdrModuleWrapper (hProcess, (MemoryAddress)vaList [index] - 16);
				//  memObjects.Add (hvaw.ModuleWrapper);
        //}

        #endregion
      }
      return memObjects;
    }

    #endregion
  }
}
